package cn.tansanqinger.dao.sql.account;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.IUserSqlDao;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;
/**
 * 用户数据真实查询类
 * @author C3H2
 * @time 2021.01
 */
@Repository
public class UserAccountSqlDaoImpl {
	@Autowired
	private IUserSqlDao sqlDao;
	
	/**
	 * 筛选用户
	 * @param pageData
	 * @return
	 */
	public PageData selectScreenAccount(PageData pageData, int jurisdiction) {
		//是否有关键字
		String keywords = Tools.isEmpty(pageData.get("keywords"));
		//查询的数据信息
		String status = Tools.isEmpty(pageData.getString("status"));
		//进行分页处理
		int page = 1;
		if(!"".equals(Tools.isEmpty(pageData.get("page")))) {
		 page = Integer.parseInt(Tools.isEmpty(pageData.get("page")));
		}
		int limit = 10000;
		if(!"".equals(Tools.isEmpty(pageData.get("limit")))) {
			limit = Integer.parseInt(Tools.isEmpty(pageData.get("limit")));
		}
		QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
		if(jurisdiction!=-1) {
			queryWrapper.lambda().eq(User::getJurisdiction, jurisdiction);
		}
		if(!"".equals(status)) {
			//被举报
			if("1".equals(status)) queryWrapper.lambda().eq(User::getIsreport, 1);
			//申述中
			if("2".equals(status)) queryWrapper.lambda().eq(User::getIsstate, 1);
			//被封禁
			if("3".equals(status)) queryWrapper.lambda().eq(User::getIsbanned, 1);
		}
		if(!"".equals(keywords)) {
			queryWrapper.and(wrapper -> wrapper.like("email", keywords).or().
					like("phone", keywords).or().like("name", keywords));
		}
		pageData = new PageData();
		pageData.put("count", sqlDao.selectCount(queryWrapper));
		Page<User> pg = new Page<User>(page, limit);
		IPage<User> iPage = sqlDao.selectPage(pg, queryWrapper);
		pageData.put("data", iPage.getRecords());
		return pageData;
	}
	
	public PageData selectTeacherAccount(PageData pageData, int dispose) {
		//是否有关键字
		String keywords = Tools.isEmpty(pageData.get("keywords"));
		//查询的数据信息
		String status = Tools.isEmpty(pageData.getString("status"));
		//进行分页处理
		int page = Integer.parseInt(Tools.isEmpty(pageData.get("page")));
		int limit = Integer.parseInt(Tools.isEmpty(pageData.get("limit")));
		QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
		queryWrapper.lambda().eq(User::getJurisdiction, 1).eq(User::getIsactivate, dispose);
		if(!"".equals(status)) {
			//被举报
			if("1".equals(status)) queryWrapper.lambda().eq(User::getIsreport, 1);
			//申述中
			if("2".equals(status)) queryWrapper.lambda().eq(User::getIsstate, 1);
			//被封禁
			if("3".equals(status)) queryWrapper.lambda().eq(User::getIsbanned, 1);
		}
		if(!"".equals(keywords)) {
			queryWrapper.and(wrapper -> wrapper.like("email", keywords).or().
					like("phone", keywords).or().like("name", keywords));
		}
		pageData = new PageData();
		pageData.put("count", sqlDao.selectCount(queryWrapper));
		Page<User> pg = new Page<User>(page, limit);
		IPage<User> iPage = sqlDao.selectPage(pg, queryWrapper);
		pageData.put("data", iPage.getRecords());
		return pageData;
	}
	
	/**
	 * 通过id直接查询单一数据
	 * @param id
	 * @return
	 */
	public User selectId(int id) {
		return sqlDao.selectById(id);
	}

	/**
	 * 通过id修改账号是否被封禁的信息
	 * @param user
	 * @return
	 */
	public int updateBannedId(User user) {
		return sqlDao.updateById(user);
	}
}
